<?php
//抽奖类
class luckClassAction extends Action
{   
    
    //抽取项目列表
	public function getCqzzListAjax(){
		$cqid  = $this->request("cqid");
		$name  = $this->request("name");
		$title = $this->request("title");
		$page  = $this->request('page', 1);
        $limit = $this->request('limit', 15);
        $whereStr = "1=1";
        if ($name) {
			$whereStr .= " and b.name like '%".$name."%'";
		}
		if ($title) {
			$whereStr .= " and a.title like '%".$title."%'";
		}

		$sql   = 'SELECT a.id FROM `[Q]a_cqzz` as a left join `[Q]zbcg_project_summary` as b on a.pid=b.id where '.$whereStr;
		$listSql = 'SELECT a.*,b.name,b.mergename FROM `[Q]a_cqzz` as a inner join `[Q]zbcg_project_summary` as b on a.pid=b.id where '.$whereStr.' order by a.id desc '.' limit '.($page-1)*$limit.','.$limit;
		$number = $this->db->getall($sql);
        $list   = $this->db->getall($listSql);
        foreach ($list as $key => $value) {
        	if($value['type']==2){
                $list[$key]['type_text'] = "已完成";
        	}else{
        		$list[$key]['type_text'] = "进行中";
        	}
        	if(!empty($value['mergename'])){
				$list[$key]['name'] = $value['mergename'];
        	}
        }
		$bacarr	= array(
            'totalCount'=> count($number),
            'rows'		=> $list
        );
        return json_encode($bacarr);
	}

	//查询今年的项目(合并的项目作为一个项目)
	public function getNowYearMergeProjectAjax(){
        $Sql = "select id,projectid,name,companyname,mergeids,mergename,no from [Q]zbcg_project_summary where status=1 and addtime>'".date("Y-01-01 01:01:01")."'order by addtime desc";
        $list = $this->db->getall($Sql);
        $name = "";
        foreach ($list as $key => $value) {
        	if(!empty($value['mergeids']) && !empty($value['mergename'])){
        		if($name==$value['mergename']){
                    unset($list[$key]);
        		}else{
                    $name = $value['mergename'];
                    $list[$key]['name'] = $value['mergename']."(合并后项目)";
        		}
        		if(empty($name) || $name==""){
                	$name = $value['mergename'];
                	$list[$key]['name'] = $value['mergename']."(合并后项目)";
                }
        	}
        	if(!empty($value['no']) && !empty($value['mergename']) && empty($value['mergeids'])){
                $list[$key]['name'] = $value['name']."(拆包出来的项目)";
        	}
        }
		echo json_encode($list);
	}


	//获取抽取专家记录
	public function getCqzzLogInfoAjax($id){
		if(empty($id) || $id<1){
            $id = $this->request("id");
		}
		$sql  = 'SELECT a.uid,b.name FROM `[Q]a_cqzz_log` as a left join `[Q]admin` as b on a.uid=b.id where a.cqid='.$id.'  order by a.id desc ';
		$list = $this->db->getall($sql);
		return $list;
	}
    
    //获取所有专家
	public function getZhuanjiaListAjax(){
		$sql = 'SELECT id,name,weight,tel FROM `[Q]a_zhuanjia`  where 1=1 order by id desc ';
		$list = $this->db->getall($sql);
		return $list;
	}
	//获取所有专家类型
	public function getZhuanjiaTypeListAjax(){
		$sql = 'SELECT id,title as name FROM `[Q]a_zhuanjia_type`  where 1=1 order by id desc ';
		$list = $this->db->getall($sql);
		return $list;
	}

	//获取所有专家
	public function zhuanjialist($zztype){
		if(empty($zztype)){
            $zztype  = $this->request("zztype");
		}
		$wherestr = "1=1"; 
		if($zztype>0){
            $wherestr.= " and a.type=".$zztype; 
		}else{
			return json_encode("请选择专家分类");
		}
		$sql = 'SELECT a.type,b.id,b.name,b.weight FROM `[Q]a_zj_type` as a inner join `[Q]a_zhuanjia` as b on a.zid=b.id where '.$wherestr.' order by id desc ';
		$list = $this->db->getall($sql);
		return $list;
	}

	//抽取专家列表
	public function getCqzzLogListAjax(){
		$cqid  = $this->request("cqid");
		$name  = $this->request("name");
		$title = $this->request("title");
		$page  = $this->request('page', 1);
        $limit = $this->request('limit', 15);
        $whereStr = "1=1";
        $whereStr .= " and a.cqid = ".$cqid;
        if ($name) {
			$whereStr .= " and c.name like '%".$name."%'";
		}
		if ($title) {
			$whereStr .= " and b.title like '%".$title."%'";
		}

		$sql   = 'SELECT a.id FROM `[Q]a_cqzz_log` as a left join `[Q]a_cqzz` as b on a.cqid=b.id left join `[Q]a_zhuanjia` as c on a.uid=c.id where '.$whereStr;
		$listSql = 'SELECT a.*,b.pid,b.title,c.name,c.tel FROM `[Q]a_cqzz_log` as a left join `[Q]a_cqzz` as b on a.cqid=b.id left join `[Q]a_zhuanjia` as c on a.uid=c.id  where '.$whereStr.' order by a.type asc,a.id desc '.' limit '.($page-1)*$limit.','.$limit;
		$number = $this->db->getall($sql);
        $list   = $this->db->getall($listSql);
        foreach ($list as $key => $value) {
        	if($value['type']==2){
                $list[$key]['type_text'] = "作废";
        	}else{
        		$list[$key]['type_text'] = "正常";
        	}
        	$typetext = $this->db->getone("[Q]a_zhuanjia_type", "id=".$value['classify'],'title');
        	$list[$key]['classify_text'] = $typetext['title'];
        }
		$bacarr	= array(
            'totalCount'=> count($number),
            'rows'		=> $list
        );
        return json_encode($bacarr);
	}
    
    //抽取列表
	public function cqzzlog($id){
		if(empty($id) || $id<1){
            $id = $this->request("id");
		}
		$sql  = 'SELECT a.uid,b.name FROM `[Q]a_cqzz_log` as a inner join `[Q]a_zhuanjia` as b on a.uid=b.id where a.cqid='.$id.'  order by a.id desc ';
		$list = $this->db->getall($sql);
		return $list;
	}

	//抽取记录列表
	public function chouqulogAjax(){
        $id = $this->request("id");
		$sql  = 'SELECT a.*,b.name FROM `[Q]a_cqzz_log` as a left join `[Q]a_zhuanjia` as b on a.uid=b.id where a.cqid='.$id.'  order by a.id desc ';
		$list = $this->db->getall($sql);
		echo json_encode($list);exit;
	}
    
    //删除抽取专家
	public function shanchuAjax(){
		$id = $this->request("id");
		m('a_cqzz')->delete("id=".$id);
		m('a_cqzz_log')->delete("cqid=".$id);
		echo json_encode("删除成功");
	}
	
	//发起抽取项目
	public function faqiAjax(){
		$title = $this->request("title");
		$pid   = $this->request("pid");
		$progect = $this->db->getone("[Q]a_cqzz", "pid=".$pid);
		if(!empty($progect)){
            echo json_encode("该项目已抽取！");exit;
		}
        $new = array(
			"sum"=>0,
			"uid"=>$this->adminid,
			"pid"=>$pid,
			"title"=>$title,
			"type"=>1,
			"addtime"=>date("Y-m-d H:i:s")
		);
        m('a_cqzz')->insert($new);
        $id	= $this->db->insert_id();
        echo json_encode("发起成功");
	}

	//执行抽取专家
	public function zhixingAjax(){
		$num    = (int)$this->request("num");
		$zztype = $this->request("zztype");
        $id	    = $this->request("id");
        $a_cqzz = $this->db->getone("[Q]a_cqzz", "id=".$id);
		if(!empty($a_cqzz) && $a_cqzz['type']==2){
            echo json_encode("该项目已完成抽取不能继续操作！");exit;
		}
        if(!empty($num) && $num>0){
        	for ($i=0; $i < $num; $i++) { 
        		$this->chouquzhuanjia($zztype,$id,$i);
        	}
            echo json_encode("抽取成功");
        }else{
        	$this->chouquzhuanjia($zztype,$id,0);
        	echo json_encode("抽取成功");
        }
	}

	public function chouquzhuanjia($zztype,$id,$i){
        //获取所有专家
        $info = $this->zhuanjialist($zztype);
        //查询已抽专家
        $log  = $this->cqzzlog($id);
        //去除已抽取的专家
        $ids     = [];
        foreach ($log as $key => $value) {
        	$ids[] = $value['uid'];
        }
        foreach ($info as $k => $v) {
        	if(in_array($v['id'], $ids)){
               unset($info[$k]);
        	}
        }
        if(empty($info) || $info==""){
            echo json_encode("已抽取".$i."人，没有可继续抽取的专家");exit;
        }
        $res  = $this->arraydata($info);
        if(!empty($res) && $res>0){
                $new = array(
                "cqid"=>$id,
                "uid"=>$res,
                "type"=>1,
                "classify"=>$zztype,
                "addtime"=>date("Y-m-d H:i:s")
            );
            m('a_cqzz_log')->insert($new);
            $cqzz = m('a_cqzz')->getone('id='.$id);
            $sum  = $cqzz['sum']+1;
            m('a_cqzz')->update(array('sum'=>$sum), 'id='.$id);
            return 1;
        }else{
            echo json_encode("已抽取".$i."人，没有可继续抽取的专家");exit;
        }
        
	}

	//作废
	public function zuofeiAjax(){
		$id      = $this->request("id");
		$cqid    = $this->request("cqid");
		$remarks = $this->request("remarks");
		$a_cqzz = $this->db->getone("[Q]a_cqzz", "id=".$cqid);
		if(!empty($a_cqzz) && $a_cqzz['type']==2){
            echo json_encode("该项目已完成抽取不能继续操作！");exit;
		}
        m('a_cqzz_log')->update(array('remarks'=>$remarks,'type'=>2,'save'=>date("Y-m-d H:i:s")), 'id='.$id);
        echo json_encode("作废");
	}

	//放大数组后返回随机
	private function arraydata($arr){

		if(empty($arr)){
            $arr = '';
		}
        $ret  = [];
        foreach ($arr as $key => $value) {
        	if(!empty($value['weight']) &&  intval($value['weight'])>0){
                for($i=0;$i<intval($value['weight']);$i++){
                    $ret[] = $value['id'];
	        	}
        	}
        }
	    //重新排序
        if(!empty($ret) && $ret!=""){
            shuffle($ret);
            $res = array_rand($ret);
            return intval($ret[$res]);
        }else{
            return 0;
        }
	}
    
    //标记完成
	public function wanchengAjax(){
        $id  = $this->request("id");
        m('a_cqzz')->update(array('type'=>2,'endtime'=>date("Y-m-d H:i:s")), 'id='.$id);
        echo json_encode("标记成功");
	}

}